package cn.lliiooll;

import cn.lliiooll.cmd.BsCmd;
import cn.lliiooll.cmd.MainCmd;
import cn.lliiooll.cmd.StCmd;
import cn.lliiooll.listener.MainListener;
import cn.lliiooll.papi.PapiHook;
import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;

public class Mentor extends JavaPlugin {

    /**
     *
     *
     *   目前BUG有:
     *   1、不能写入文件
     *   2、PAPI变量错误
     *
     */

    String s = new String("" +
            "此插件已开源，你不必反编译" +
            "开源地址:" +
            "https://gitee.com/lliioollcn/Mentor" +
            "by: lliiooll" +
            "");

    public static Mentor m;

    @Override
    public void onLoad(){
        m = this;
        this.info("插件启动ing...");
    }

    @Override
    public void onEnable(){
        this.info("注册指令ing...");
        this.regCmd();
        this.info("注册事件ing...");
        this.regEvt();
        this.info("检测配置ing...");
        if(!(new File(Mentor.m.getDataFolder(),"config.yml").exists())){
            Mentor.m.info("没有检测到配置,释放ing...");
            Mentor.m.saveDefaultConfig();
        }else{
            Mentor.m.info("检测到配置,加载ing...");
            Mentor.m.reloadConfig();
        }
        this.info("Hooked PAPIing...");
        if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null){
            if(new PapiHook().hook()) info("Hook PAPI成功"); else this.warn("Hook PAPI失败了，将不启用占位符");
        }else{
            info("PAPI不存在，不进行Hook");
        }

        this.info("加载完毕了(〃'▽'〃)");
    }

    @Override
    public void onDisable(){
        this.info("插件关闭完毕");
    }


    private void regCmd(){
        Bukkit.getPluginCommand("mentor").setExecutor(new MainCmd());
        Bukkit.getPluginCommand("bs").setExecutor(new BsCmd());
        Bukkit.getPluginCommand("st").setExecutor(new StCmd());
    }

    private void regEvt(){
        Bukkit.getPluginManager().registerEvents(new MainListener(),this);
    }

    public static String getPluginName(){
        return "mentor";
    }
    public static String getPluginVersion(){
        return "1.0.0";
    }

    private static String pre = "&c&l[&bMentor&c&l]&f".replaceAll("&","§");
    private String  custompre = getConfig().getString("customprefix").replaceAll("&","§");
    private static ConsoleCommandSender c =  Bukkit.getConsoleSender();

    public void info(String msg){
        c.sendMessage(pre + " §b" + msg.replaceAll("&","§"));
    }

    public void info(String msg, Player p){
        p.sendMessage(custompre + " §b" + msg.replaceAll("&","§"));
    }

    public void warn(String msg){
        c.sendMessage(pre + " §e" + msg.replaceAll("&","§"));
    }

    public void warn(String msg, Player p){
        p.sendMessage(custompre + " §e" + msg.replaceAll("&","§"));
    }

    public void error(String msg){
        c.sendMessage(pre + " §c" + msg.replaceAll("&","§"));
    }

    public void error(String msg, Player p){
        p.sendMessage(custompre + " §c" + msg.replaceAll("&","§"));
    }

    public void debug(String msg){
        c.sendMessage(pre + " §b[Debug] §e" + msg.replaceAll("&","§"));
    }

    public void broadcastMessage(String msg){
        Bukkit.broadcastMessage(custompre + " §b" + msg.replaceAll("&","§"));
    }
    public void broadcast(String msg,String permission){
        Bukkit.broadcast(custompre + " §b" + msg.replaceAll("&","§"),permission);
    }
}
